﻿using OmronFinsTCP.Net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace ESS280Ah_TestTool
{
    public partial class Form_Presure : Form
    {
        public string form1zhiip;
        public string form1zhiport;
        EtherNetPLC ENT_01;
        public bool connectFlag = false;
        public bool readParamOK = false;
        string[] realTimeReal={ "20", "22", "24", "26", "40", "42", "44", "46" };
        float[] paramReal = new float[40];//{ 0,0,0,0,0,0,0,0,0,0,0 };
        short[] iReal;//= { "20", "22", "24", "26", "40", "42", "44", "46" };
        public bool firstReadflag = false;

        private BackgroundWorker bgWorker = new BackgroundWorker();
        public Form_Presure()
        {
            InitializeComponent();
            InitializeBackgroundWorker();
        }
        public string Form1ChuanIP
        {
            get
            {
                return form1zhiip;
            }
            set
            {
                form1zhiip = value;
            }
        }
        public string Form1ChuanPort
        {
            get
            {
                return form1zhiport;
            }
            set
            {
                form1zhiport = value;
            }
        }
        private void InitializeBackgroundWorker()
        {
            bgWorker.WorkerReportsProgress = true;
            bgWorker.WorkerSupportsCancellation = true;
            bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
            bgWorker.ProgressChanged += new ProgressChangedEventHandler(bgWorker_ProgessChanged);
            bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_WorkerCompleted);
        }
        public void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; true; )
            {
                if (bgWorker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
                else
                {
                    bgWorker.ReportProgress(i, "Working");
                    System.Threading.Thread.Sleep(100);
                    plc01_readRealTimeFloat();
                    firstReadflag = true;
                }
            }
        }
        public void bgWorker_ProgessChanged(object sender, ProgressChangedEventArgs e)
        {
            //string state = (string)e.UserState;//接收ReportProgress方法传递过来的userState
            //this.progressBar1.Value = e.ProgressPercentage;
            //this.label1.Text = "处理进度:" + Convert.ToString(e.ProgressPercentage) + "%";
            //for (int i = 0; true; )
            //{

            
            if (firstReadflag)
            {

            
            textBox_P1.Text = form1zhiip;
            textBox_P2.Text = form1zhiport;
            textBox_P1.Text = realTimeReal[0];
            textBox_P2.Text = realTimeReal[1];
            textBox_P3.Text = realTimeReal[2];
            textBox_P4.Text = realTimeReal[3];
            textBox_P5.Text = realTimeReal[4];
            textBox_P6.Text = realTimeReal[5];
            textBox_P7.Text = realTimeReal[6];
            textBox_P8.Text = realTimeReal[7];
            textBox_i1.Text = iReal[0].ToString();
            textBox_i2.Text = iReal[1].ToString();
            textBox_i3.Text = iReal[2].ToString();
            textBox_i4.Text = iReal[3].ToString();
            textBox_i5.Text = iReal[4].ToString();
            textBox_i6.Text = iReal[5].ToString();
            textBox_i7.Text = iReal[6].ToString();
            textBox_i8.Text = iReal[7].ToString();
                    //System.Threading.Thread.Sleep(500);

            //    }
            }
            //           for (int i = 0; i >= 0;)
            //           {
            //               string j = plc01_readFloat(1, 1);
                           
            //           }
        }
        public void bgWorker_WorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.ToString());
                return;
            }
            if (!e.Cancelled)
                ;
            else
                ;
        }
        private void button1_Click(object sender, EventArgs e)
        {
        }
        private void button2_Click(object sender, EventArgs e)
        {
            bgWorker.CancelAsync();
        }
        public bool plc01_connect(int con)
        {
            bool conOK = false;
            string s = btnCon_01.Text.Trim();
            if (s == "连接")
            {
                string plcip = form1zhiip;
                string plcport = form1zhiport;
                ENT_01 = new EtherNetPLC();
                short re = ENT_01.Link(plcip, short.Parse(plcport), 500);
                if (re == 0)
                {
                    btnCon_01.Text = "断开";
                    connectFlag = true;
                    conOK = true;
                }
                else
                {
                    MessageBox.Show("连接出错！");
                }
            }
            else
            {
                conOK = true;
                //con用于判断是否点击断开按钮
                if (con == 1)
                {
                    bgWorker.CancelAsync();
                    short re = ENT_01.Close();
                    if (re == 0)
                    {
                        btnCon_01.Text = "连接";
                        connectFlag = false;

                    }
                    else
                    {
                        MessageBox.Show("断开出错！");
                    }
                }
            }
            return conOK;
        }
        public void plc01_readRealTimeFloat()
        {
            float rd=0;
            string[] addr = { "20", "22", "24", "26", "40", "42", "44", "46" };
            short re;
            for (int i = 0; i < 8; i++)
            {
                re = ENT_01.ReadReal(PlcMemory.DM, short.Parse(addr[i]), out rd);
                if (re != 0)
                {
                    //写入数据出错
                    realTimeReal[i] = "数据读取出错！";
                }
                else
                {
                    realTimeReal[i] = rd.ToString();
                }
                System.Threading.Thread.Sleep(100);
            }
            //string[] addr2 = { "8", "9", "10", "11", "12", "13", "14", "15" };
            //short rw;
            ENT_01.ReadWords(PlcMemory.CIO, 8,8, out iReal);
            //for (int i = 0; i < 8; i++)
            //{
            //    re = ENT_01.ReadWord(PlcMemory.CIO, short.Parse(addr[i]), out rw);
            //    if (re != 0)
            //    {
            //        //写入数据出错
            //        iReal[i] = "数据读取出错！";
            //    }
            //    else
            //    {
            //        iReal[i] = rw.ToString();
            //    }
            //    System.Threading.Thread.Sleep(100);
            //}
        }
        public void plc01_readParamFloat()
        {
            float rd;
            string[] addr = { "1200", "1202", "1204", "1206", "1208", "1210", "1212", "1214", "1216", "1218", "1220", "1222", "1224", "1226", "1228", "1230", "1232", "1234", "1236", "1238", "1240", "1242", "1244", "1246", "1248", "1250", "1252", "1254", "1256", "1258", "1260", "1262", "1264", "1266", "1268", "1270", "1272", "1274", "1276", "1278" };
            short re;
            
            for (int i = 0; i < addr.Count(); i++)
            {
                re = ENT_01.ReadReal(PlcMemory.DM, short.Parse(addr[i]), out rd);
                if (re != 0)
                {
                    //写入数据出错
                    //pReal[i] = "数据读取出错！";
                }
                else
                {
                    //pReal[i] = rd.ToString();
                }
                System.Threading.Thread.Sleep(100);
            }

        }
        private void Form_Presure_Load(object sender, EventArgs e)
        {
            //if (plc01_connect(0))
            //{
            //    if (bgWorker.IsBusy)
            //        return;

            //    bgWorker.RunWorkerAsync("hello");
            //}
            Label[] lblArray = { new Label(), new Label() };

            //设置属性
            lblArray[0].Location = new System.Drawing.Point(132, 280);
            lblArray[0].Name = "label_0";
            lblArray[0].Size = new System.Drawing.Size(64, 16);
            lblArray[0].TabIndex = 1;
            lblArray[0].Text = "label_0";

            lblArray[1].Location = new System.Drawing.Point(232, 280);
            lblArray[1].Name = "label_1";
            lblArray[1].Size = new System.Drawing.Size(64, 16);
            lblArray[1].TabIndex = 2;
            lblArray[1].Text = "label_1";

            //最后一条语句尤其重要，如果不写，则控件不能在窗体上显示。
            this.Controls.AddRange(lblArray);




        }
        private void Form_Presure_FormClosed(object sender, FormClosedEventArgs e)
        {
            //bgWorker.CancelAsync();
            //ENT_01.Close();
        }

        private void button_ReadP_Click(object sender, EventArgs e)
        {
            float rdr = 0;

            //ENT_01.ReadReal(PlcMemory.DM, 1212, out rdr);
            //textBoxA1P1p.Text = rdr.ToString();
            //ENT_01.ReadReal(PlcMemory.CIO, 8, out rdr);
            //textBoxA1P1i.Text = rdr.ToString();
                
            //short rds;
            //ENT_01.ReadWord(PlcMemory.CIO, 8,out rds);
            //textBoxA1P2i.Text = rds.ToString();
            //short[] rdss;
            //ENT_01.ReadWords(PlcMemory.CIO, 8,2,out rdss);
            //textBoxA2P1i.Text = rdss[0].ToString();
            //textBoxA2P1p.Text = rdss[1].ToString();


            int add = 1200;
            for (int i = 0; i < 40; i++)
            {

                ENT_01.ReadReal(PlcMemory.DM, Convert.ToInt16(add+i*2), out rdr);
                paramReal[i] = rdr;
            }
            textBoxA1P1i.Text = paramReal[1].ToString();
            textBoxA1P1p.Text = paramReal[2].ToString();
            textBoxA1P2i.Text = paramReal[3].ToString();
            textBoxA1P2p.Text = paramReal[4].ToString();

            textBoxA2P1i.Text = paramReal[6].ToString();
            textBoxA2P1p.Text = paramReal[7].ToString();
            textBoxA2P2i.Text = paramReal[8].ToString();
            textBoxA2P2p.Text = paramReal[9].ToString();

            textBoxB1P1i.Text = paramReal[11].ToString();
            textBoxB1P1p.Text = paramReal[12].ToString();
            textBoxB1P2i.Text = paramReal[13].ToString();
            textBoxB1P2p.Text = paramReal[14].ToString();

            textBoxB2P1i.Text = paramReal[16].ToString();
            textBoxB2P1p.Text = paramReal[17].ToString();
            textBoxB2P2i.Text = paramReal[18].ToString();
            textBoxB2P2p.Text = paramReal[19].ToString();

            textBoxC1P1i.Text = paramReal[21].ToString();
            textBoxC1P1p.Text = paramReal[22].ToString();
            textBoxC1P2i.Text = paramReal[23].ToString();
            textBoxC1P2p.Text = paramReal[24].ToString();

            textBoxC2P1i.Text = paramReal[26].ToString();
            textBoxC2P1p.Text = paramReal[27].ToString();
            textBoxC2P2i.Text = paramReal[28].ToString();
            textBoxC2P2p.Text = paramReal[29].ToString();

            textBoxD1P1i.Text = paramReal[31].ToString();
            textBoxD1P1p.Text = paramReal[32].ToString();
            textBoxD1P2i.Text = paramReal[33].ToString();
            textBoxD1P2p.Text = paramReal[34].ToString();

            textBoxD2P1i.Text = paramReal[36].ToString();
            textBoxD2P1p.Text = paramReal[37].ToString();
            textBoxD2P2i.Text = paramReal[38].ToString();
            textBoxD2P2p.Text = paramReal[39].ToString();

            readParamOK = true;
            button_writeP.Enabled = true;

        }

        private void button_writeP_Click(object sender, EventArgs e)
        { 
            float.TryParse(textBoxA1P1i.Text, out paramReal[1]);
            float.TryParse(textBoxA1P1p.Text, out paramReal[2]);
            float.TryParse(textBoxA1P2i.Text, out paramReal[3]);
            float.TryParse(textBoxA1P2p.Text, out paramReal[4]);

            float.TryParse(textBoxA2P1i.Text, out paramReal[6]);
            float.TryParse(textBoxA2P1p.Text, out paramReal[7]);
            float.TryParse(textBoxA2P2i.Text, out paramReal[8]);
            float.TryParse(textBoxA2P2p.Text, out paramReal[9]);

            float.TryParse(textBoxB1P1i.Text, out paramReal[11]);
            float.TryParse(textBoxB1P1p.Text, out paramReal[12]);
            float.TryParse(textBoxB1P2i.Text, out paramReal[13]);
            float.TryParse(textBoxB1P2p.Text, out paramReal[14]);

            float.TryParse(textBoxB2P1i.Text, out paramReal[16]);
            float.TryParse(textBoxB2P1p.Text, out paramReal[17]);
            float.TryParse(textBoxB2P2i.Text, out paramReal[18]);
            float.TryParse(textBoxB2P2p.Text, out paramReal[19]);

            float.TryParse(textBoxC1P1i.Text, out paramReal[21]);
            float.TryParse(textBoxC1P1p.Text, out paramReal[22]);
            float.TryParse(textBoxC1P2i.Text, out paramReal[23]);
            float.TryParse(textBoxC1P2p.Text, out paramReal[24]);

            float.TryParse(textBoxC2P1i.Text, out paramReal[26]);
            float.TryParse(textBoxC2P1p.Text, out paramReal[27]);
            float.TryParse(textBoxC2P2i.Text, out paramReal[28]);
            float.TryParse(textBoxC2P2p.Text, out paramReal[29]);

            float.TryParse(textBoxD1P1i.Text, out paramReal[31]);
            float.TryParse(textBoxD1P1p.Text, out paramReal[32]);
            float.TryParse(textBoxD1P2i.Text, out paramReal[33]);
            float.TryParse(textBoxD1P2p.Text, out paramReal[34]);

            float.TryParse(textBoxD2P1i.Text, out paramReal[36]);
            float.TryParse(textBoxD2P1p.Text, out paramReal[37]);
            float.TryParse(textBoxD2P2i.Text, out paramReal[38]);
            float.TryParse(textBoxD2P2p.Text, out paramReal[39]);

            byte[] paramRealByte=new byte [40];
            paramRealByte=BitConverter.GetBytes(paramReal[1]);


            WriteReal(PlcMemory.DM, 1200, paramReal[1]);
            int add = 1200;
            for (int i = 0; i < 40; i++)
            {
                WriteReal(PlcMemory.DM, Convert.ToInt16(1200+i*2), paramReal[i]);
            }



        }
        public void WriteReal(PlcMemory mr, short ch, float reData)
        {
            byte[] paramRealByte = new byte[40];
            paramRealByte = BitConverter.GetBytes(reData);
            short[] s = new short[40];
            int i1 = paramRealByte[1] * 0x100 + paramRealByte[0];
            s[0] = (short)i1;
            i1 = paramRealByte[3] * 0x100 + paramRealByte[2];
            s[1] = (short)i1;
            ENT_01.WriteWords(mr, ch, 2, s);




        }
    }

}


